/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.model.poison;

import static l1j.server.server.model.skill.L1SkillId.STATUS_POISON_PARALYZED;
import static l1j.server.server.model.skill.L1SkillId.STATUS_POISON_PARALYZING;
import l1j.server.Thread.GeneralThreadPool;
import l1j.server.server.model.L1Character;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_Paralysis;

public class L1ParalysisPoison extends L1Poison {
	// 麻痺毒の性能一覧 猶予 持続 (参考値、未適用)
	// グール 20 45
	// アステ 10 60
	// 蟻穴ムカデ 14 30
	// D-グール 39 45

	private class ParalysisPoisonTimer extends Thread {
		@Override
		public void run() {
			L1ParalysisPoison.this._target.setSkillEffect(STATUS_POISON_PARALYZING, 0);

			try {
				Thread.sleep(L1ParalysisPoison.this._delay); // 麻痺するまでの猶予時間を待つ。
			} catch (final InterruptedException e) {
				L1ParalysisPoison.this._target.killSkillEffectTimer(STATUS_POISON_PARALYZING);
				return;
			}

			// エフェクトを緑から灰色へ
			L1ParalysisPoison.this._effectId = 2;
			L1ParalysisPoison.this._target.setPoisonEffect(2);

			if (L1ParalysisPoison.this._target instanceof L1PcInstance) {
				final L1PcInstance player = (L1PcInstance) L1ParalysisPoison.this._target;
				if (player.isDead() == false) {
					player.sendPackets(new S_Paralysis(1, true)); // 麻痺状態にする
					L1ParalysisPoison.this._timer = new ParalysisTimer();
					GeneralThreadPool.getInstance().execute(L1ParalysisPoison.this._timer); // 麻痺タイマー開始
					if (this.isInterrupted()) {
						L1ParalysisPoison.this._timer.interrupt();
					}
				}
			}
		}
	}
	private class ParalysisTimer extends Thread {
		@Override
		public void run() {
			L1ParalysisPoison.this._target.killSkillEffectTimer(STATUS_POISON_PARALYZING);
			L1ParalysisPoison.this._target.setSkillEffect(STATUS_POISON_PARALYZED, 0);
			try {
				Thread.sleep(L1ParalysisPoison.this._time);
			} catch (final InterruptedException e) {
			}

			L1ParalysisPoison.this._target.killSkillEffectTimer(STATUS_POISON_PARALYZED);
			if (L1ParalysisPoison.this._target instanceof L1PcInstance) {
				final L1PcInstance player = (L1PcInstance) L1ParalysisPoison.this._target;
				if (!player.isDead()) {
					player.sendPackets(new S_Paralysis(1, false)); // 麻痺状態を解除する
					L1ParalysisPoison.this.cure(); // 解毒処理
				}
			}
		}
	}
	public static boolean doInfection(final L1Character cha, final int delay, final int time) {
		if (!L1Poison.isValidTarget(cha,true)) {
			return false;
		}

		cha.setPoison(new L1ParalysisPoison(cha, delay, time));
		return true;
	}
	private final L1Character _target;
	private Thread _timer;

	private final int _delay;

	private final int _time;

	private int _effectId = 1;

	private L1ParalysisPoison(final L1Character cha, final int delay, final int time) {
		this._target = cha;
		this._delay = delay;
		this._time = time;

		this.doInfection();
	}

	@Override
	public void cure() {
		if (this._timer != null) {
			this._timer.interrupt(); // 麻痺毒タイマー解除
		}

		this._target.setPoisonEffect(0);
		this._target.setPoison(null);
	}

	private void doInfection() {
		sendMessageIfPlayer(this._target, 212);
		this._target.setPoisonEffect(1);

		if (this._target instanceof L1PcInstance) {
			this._timer = new ParalysisPoisonTimer();
			GeneralThreadPool.getInstance().execute(this._timer);
		}
	}

	@Override
	public int getEffectId() {
		return this._effectId;
	}
}
